subroutine add_external_event(lunit,eflag)
  use global_param,only:n_ext
  use global_param,only:extern_type
  use event_param,only:extn_event
  use profile_map_param,only:profile_map_extn
  use table_param,only:event_profile
  use table_param,only:n_event_profile
  use table_param,only:event_table
  use table_param,only:n_event_table
  use table_param,only:del_event_table
  use table_param,only:n_del_event_table
  use table_param,only:alloc_event_n_in_profile
  implicit none

  interface
     function reallocInt(p,n)
       integer,pointer,dimension(:) :: reallocInt,p
       integer,intent(in) :: n
     end function reallocInt
  end interface

  integer,intent(in) :: lunit,eflag
  
  integer :: i
  integer :: pid,eid

  do i=1,n_ext
     if (eflag.eq.0) then
        ! if external event was turned off,
        if (profile_map_extn(i).eq.0) then
           ! The profile has not been defined yet.
        else
           ! The profile has been already defined.
           pid=profile_map_extn(i)
           if (event_profile(pid)%n_event.gt.0) then
              ! The event table has been defined,
              ! then remove the event from the event table.
              eid=event_profile(pid)%event_list(1)
              n_del_event_table=n_del_event_table+1
              del_event_table(n_del_event_table)=eid
              event_table(eid)%removed=.true.
              n_event_table=n_event_table-1
              event_profile(pid)%n_event=0
           else
              ! The profile table exists, but
              ! the event table has not been defined.
           end if
        end if
     else
        ! if external event was turned on,
        if (profile_map_extn(i).eq.0) then
           ! add to the event table
           n_event_table=n_event_table+1
           call alloc_event_table(n_event_table,eid)
           event_table(eid)%event_type=extn_event
           event_table(eid)%obj1=i

           ! add to the profile table
           n_event_profile=n_event_profile+1
           call alloc_profile_table(n_event_profile,pid)
           profile_map_extn(i)=pid
           event_profile(pid)%event_type=extn_event
           event_profile(pid)%rate_const=&
                extern_type(i)%rate_const
           event_profile(pid)%n_event=1

           if (event_profile(pid)%n_event.gt.&
                event_profile(pid)%n_alloc_event) then
              event_profile(pid)%n_alloc_event=&
                   event_profile(pid)%n_alloc_event+&
                   alloc_event_n_in_profile
              event_profile(pid)%event_list=>reallocInt(&
                   event_profile(pid)%event_list,&
                   event_profile(pid)%n_alloc_event)
           end if

           ! link the event and profile
           event_profile(pid)%event_list(1)=eid
           event_table(eid)%profile_table_index=pid
        else
           pid=profile_map_extn(i)
           if (event_profile(pid)%n_event.gt.0) then
              ! Do nothing.
           else
              if (extern_type(i)%max_n_occur.le.&
                   extern_type(i)%n_occur) then
                 ! Do nothing since this event is expired.
              else
                 ! Recover the event table.
                 n_event_table=n_event_table+1
                 call alloc_event_table(n_event_table,eid)
                 event_table(eid)%obj1=i
                 event_profile(pid)%n_event=1
                 event_profile(pid)%event_list(1)=eid
                 event_table(eid)%profile_table_index=pid
              end if
           end if
        end if
     end if
  end do

  return

end subroutine add_external_event
